import numpy as np
from random import shuffle


def init_randmat(SIZE):
    random_mat = [[i for i in range(SIZE)] for _ in range(SIZE)]
    random_mat = [i for i in range(SIZE)]*SIZE
    shuffle(random_mat)
    random_mat=np.array(random_mat).reshape(SIZE,SIZE)
    zero_mat=np.zeros((SIZE+2,SIZE+2),dtype=int)
    zero_mat[1:SIZE+1,1:SIZE+1]=random_mat
    zero_mat[0,]=zero_mat[-1,]=zero_mat[:,0]=zero_mat[:,-1]=-1
    random_mat=zero_mat
    print(random_mat)
    return random_mat


def if_connected(x, y, random_mat, SIZE):
    '''
    判断x,y两点是否连通
    '''
    if random_mat[x]!=random_mat[y] or x==y:
        return
    # 相贴
    if x[0] == y[0] and abs(x[1] - y[1]) == 1 or x[1] == y[1] and abs(x[0] - y[0]) == 1:
        print(1)
        return True
    # 直线
    if x[0]==y[0] and x[1]>y[1] or x[1]==y[1] and x[0]>y[0]:
        x,y=y,x
    if x[0] == y[0] and (random_mat[x[0], x[1] + 1:y[1]] == -1).all() or x[1] == y[1] and (
            random_mat[x[0] + 1:y[0], x[1]] == -1).all():
        print(2,random_mat[x[0], x[1] + 1:y[1]])
        return True

    # 一弯
    def one_corner(x, y):
        if x[0] > y[0]:
            x, y = y, x
        if x[1] < y[1]:
            if (random_mat[x[0], x[1] + 1:y[1] + 1] == -1).all() and (random_mat[x[0] + 1:y[0], y[1]] == -1).all() \
                    or (random_mat[y[0], x[1]:y[1]] == -1).all() and (random_mat[x[0] + 1:y[0], x[1]] == -1).all():
                return True
        elif x[1] > y[1]:
            if (random_mat[x[0], y[1]:x[1]] == -1).all() and (random_mat[x[0] + 1:y[0], y[1]] == -1).all() \
                    or (random_mat[y[0], y[1] + 1:x[1] + 1] == -1).all() and (
                    random_mat[x[0] + 1:y[0], x[1]] == -1).all():
                return True

    if one_corner(x, y):
        print(3)
        return True
    # 二弯,点的气遍历加一弯判断
    if x[0]>y[0]:
        x,y=y,x
    # 上
    cnt = 1
    if x[0] > 0:
        while x[0] - cnt >= 0 and random_mat[x[0] - cnt, x[1]] == -1:
            xup = (x[0] - cnt, x[1])
            oc = one_corner(xup, y)
            if oc:
                print(4.1)
                return True
            cnt += 1
    # 下
    cnt = 1
    if x[0] < SIZE + 1:
        while x[0] + cnt <= SIZE + 1 and random_mat[x[0] + cnt, x[1]] == -1:
            xbottom = (x[0] + cnt, x[1])
            oc = one_corner(xbottom, y)
            if oc:
                print(4.2)
                return True
            cnt += 1
    # 左
    cnt = 1
    if x[1] > 0:
        while x[1] - cnt >= 0 and random_mat[x[0], x[1] - cnt] == -1:
            xleft = (x[0], x[1] - cnt)
            oc = one_corner(xleft, y)
            if oc:
                print(4.3)
                return True
            cnt += 1
    # 右
    cnt = 1
    if x[1] < SIZE + 1:
        print('hh:',x,SIZE+1)
        while x[1] + cnt <= SIZE + 1 and random_mat[x[0], x[1] + cnt] == -1:
            xright = (x[0], x[1] + cnt)
            oc = one_corner(xright, y)
            if oc:
                print(4.4)
                return True
            cnt += 1


if __name__=='__main__':
    judge_connected(6)